package com.work.servlet;

import com.work.dao.GoodsDao;
import com.work.dao.GoodsDaoImpl;
import com.work.pojo.Goods;


import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @ClassName: DetailServlet
 * @Description: TODO
 * @author: meyolo
 * @date: 2021/3/20  19:17
 * 商品详情Servlet
 */
@WebServlet(name = "requestInfo", urlPatterns = "/detail/*")
public class DetailServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获得Servlet的路径，拿到商品goods_id
        String pathInfo = request.getPathInfo();
        String[] split = pathInfo.split("/");
        String string = split[1].toString();
        int id = Integer.parseInt(string);

        GoodsDao goodsDao = new GoodsDaoImpl();
        Goods goods = goodsDao.queryGoodById(id);

        request.setAttribute("goods", goods);

        request.getRequestDispatcher("/detail.jsp").forward(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
